﻿using Sundry.IDAL;
using Sundry.Model;
using System;
using System.Collections.Generic;
using System.Data.Entity.Infrastructure;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;

namespace Sundry.DAL
{
    public class AdminLoginLogRepository : BaseRepository<AdminLoginLog>, IAdminLoginLogRepository
    {
        public override AdminLoginLog AddEntity(AdminLoginLog entity)
        {
            db.Entry<AdminLoginLog>(entity).State = System.Data.Entity.EntityState.Added;//添加记录
            //修改用户上次登录信息
            Admin admin = new Admin()
            {
                AdminId = entity.AdminId,
                AdminLastLoginAddress = entity.AdminLoginAddress,
                AdminLastLoginIP = entity.AdminLoginIP,
                AdminLastLoginTime = entity.AdminLoginTime
            };

            DbEntityEntry<Admin> adminEntry = db.Entry<Admin>(admin);
            adminEntry.State = System.Data.Entity.EntityState.Unchanged;
            adminEntry.Property("AdminLastLoginAddress").IsModified = true;
            adminEntry.Property("AdminLastLoginIP").IsModified = true;
            adminEntry.Property("AdminLastLoginTime").IsModified = true;
            return entity;
        }
        /// <summary>
        /// 添加用户登录记录
        /// </summary>
        /// <param name="entity">登录日志entity</param>
        /// <param name="admin"></param>
        /// <returns></returns>
        public AdminLoginLog AddEntity(AdminLoginLog entity, Admin admin)
        {
            db.Entry<AdminLoginLog>(entity).State = System.Data.Entity.EntityState.Added;//添加记录
            DbEntityEntry<Admin> adminEntry = db.Entry<Admin>(admin);
            adminEntry.State = System.Data.Entity.EntityState.Unchanged;
            adminEntry.Property("AdminLastLoginAddress").IsModified = true;
            adminEntry.Property("AdminLastLoginIP").IsModified = true;
            adminEntry.Property("AdminLastLoginTime").IsModified = true;
            return entity;
        }
        /// <summary>
        /// 用户登录日志列表
        /// </summary>
        /// <param name="userName">用户名</param>
        /// <param name="nowNumber">当前数字</param>
        /// <param name="pageSize">长度</param>
        /// <param name="rowCount">总行数</param>
        /// <returns>用户登录日志列表</returns>
        public IQueryable<dynamic> LoadLogList(string userName, int nowNumber, int pageSize, out int rowCount)
        {
            //var result = null;
            var result = from a in db.Set<AdminLoginLog>()
                         join b in db.Set<Admin>() on a.AdminId equals b.AdminId into b_join
                         from b in b_join.DefaultIfEmpty()
                         orderby
                           a.AdminLoginTime descending
                         select new
                         {
                             AdminId = (Int32?)a.AdminId,
                             AdminLoginAddress = a.AdminLoginAddress,
                             a.AdminLoginBrowser,
                             a.AdminLoginIP,
                             a.AdminLoginLogId,
                             a.AdminLoginTime,
                             AdminUserName = b.AdminUserName
                         };
            if (!string.IsNullOrEmpty(userName))
               result=result.Where(m => m.AdminUserName.Contains(userName));
            rowCount = result.Count();
            result = result.Skip(nowNumber).Take(pageSize);
            return result;
        }
    }
}
